// pages/login/bind-phone/phone.js
import { $wuxDialog } from '../../../wux-components/index'
const T = getApp().globalData.T
const config = getApp().globalData.config
let token = ''
Page({

  /**
   * 页面的初始数据
   */
  data: {
    item1: '',
    item2: '',
    item3: '',
    item4: '',
    focusIndex: 1,
    outTime: 60,
    isWait: false,
    phone: 0,
    time: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    token = getApp().globalData.token;
    this.data.phone = options.phone;
    this.getCode();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  onInputItem1(e) {
    this.data.item1 = e.detail.value;
    if (this.data.item1.toString()) {
      this.setData({ focusIndex: 2 })
    }
  },
  onInputItem2(e) {
    this.data.item2 = e.detail.value;
    if (this.data.item2.toString()) {
      this.setData({ focusIndex: 3 })
    } else {
      this.setData({ focusIndex: 1 })
    }
  },
  onInputItem3(e) {
    this.data.item3 = e.detail.value;
    if (this.data.item3.toString()) {
      this.setData({ focusIndex: 4 })
    } else {
      this.setData({ focusIndex: 2 })
    }
  },
  onInputItem4(e) {
    this.data.item4 = e.detail.value;
    if (!this.data.item4.toString()) {
      this.setData({ focusIndex: 3 })
    }
  },
  onUnload: function () {
    console.log('onUnload');
    let _t = this.data.time;
    if (_t) {
      clearInterval(_t);
    }
  },

  onHide: function () {
    let _t = this.data.time;
    if (_t) {
      clearInterval(_t);
    }
  },

  /**
   * 获取验证码
   */
  getCode() {
    T.req({
      loadTitle: '请求中...',
      url: config.api + 'code',
      method: 'GET',
      data: {
        token: token,
        mobile: this.data.phone,
        operate: 3
      },
      success: (res) => {
        this.setData({
          isWait: true
        });
        this.data.time = setInterval(() => {
          let _t = this.data.outTime;
          if (_t <= 0) {
            this.data.outTime = 60;
            clearInterval(this.data.time);
            this.setData({
              isWait: false
            })
            return;
          }
          _t -= 1;
          this.setData({
            outTime: _t
          });
        }, 1000)
      }
    })
  },

  /**
   * 重新发送验证码
   */
  onAgainSendCode() {
    this.getCode();
  },

  onIsBindPhone() {
    T.req({
      url: config.api + 'isbind',
      method: 'GET',
      data: {
        token: token,
        mobile: this.data.phone
      },
      success: (res) => {
        if (res.isbind) {
          // 绑定过
          this.onIsUpdatedInfo();
        } else {
          this.onBindPhone()
        }
      }
    })
  },

  onBindPhone() {
    let _code = this.data.item1 + this.data.item2 + this.data.item3 + this.data.item4;
    T.req({
      loadTitle: '请求中...',
      url: config.api + 'binding?token=' + token,
      method: 'POST',
      data: {
        mobile: this.data.phone,
        code: _code
      },
      success: (res) => {
        wx.redirectTo({
          url: '/pages/login/base-info/info'
        })
      }
    });
  },


  // 是否完善信息
  onIsUpdatedInfo() {

  }
})